<?php
/**
 * Created by PhpStorm.
 * User:tsingStar
 * Date: 2019/7/13
 * Time: 18:31
 */

namespace app\home\controller;


use app\common\model\Settings;
use think\Exception;

class Order extends Common
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 订单首页
     */
    public function index()
    {

        $this->templateTitle("订单列表");

        $this->assign("type", 1);


        return $this->fetch("index");
    }


    /**
     * 待付款
     */
    public function readyPay()
    {

        $this->templateTitle("待付款");

        $this->assign("type", 1);
        return $this->fetch("index");
    }

    /**
     * 待收获
     */
    public function receive()
    {
        $this->templateTitle("待收货");
        $this->assign("type", 2);
        return $this->fetch("index");
    }

    /**
     * 已完成
     */
    public function end()
    {
        $this->templateTitle("已完成");
        $this->assign("type", 3);
        return $this->fetch("index");
    }

    /**
     * 已取消
     */
    public function cancel()
    {
        $this->templateTitle("已取消");
        $this->assign("type", 4);
        return $this->fetch("index");
    }

    /**
     * 获取订单列表数据
     */
    public function getOrderList($type, $page, $page_num)
    {
        try {
            $list = \app\common\model\Order::orderList(session("user_id"), $type, $page, $page_num);
            if(!empty($list)){
                $this->ajaxSuccess("请求成功", $list);
            }else{
                $this->ajaxSuccess("暂无订单", $list);
            }
        } catch (\Exception $e) {
            $this->ajaxError($e->getMessage());
        }
    }


    /**
     * 填写订单
     */
    public function preOrder()
    {
        $this->templateTitle("填写订单");
        $cart_id = input("cart_id");

        $order = \app\common\model\Order::carConfirmOrder(session("user_id"), $cart_id);
        //获取默认收获地址
        if (session("address")) {
            $order["def_address"] = session("address");
            session("address", null);
        } else {
            $order["def_address"] = \app\common\model\User::getDefaultAddress(session("user_id"));
        }
        $this->assign("order", $order);
        return $this->fetch("preOrder");
    }

    /**
     * 创建订单
     */
    public function createOrder()
    {
        try {
            $cart_id = input("cart_id/a");
            if (empty($cart_id)) {
                throw new Exception("商品为空");
            }
            $cart_id = implode(",", $cart_id);
            $name = input("send_name");
            $mobile = input("send_mobile");
            $address = input("send_address");

            if (empty($name) || empty($mobile) || empty($address)) {
                throw new Exception("收货信息不能为空");
            }
            $is_score = input("is_score");
            $order_id = \app\common\model\Order::carSubOrder(session("user_id"), $cart_id, $name, $mobile, $address, $is_score);
            $redirect_url = url("Order/payInfo", ["order_id" => $order_id]);
            $this->ajaxSuccess("创建订单成功", $redirect_url);
        } catch (\Exception $e) {
            $this->ajaxError($e->getMessage());
        }
    }

    /**
     * 选择收获地址
     */
    public function selectAddress()
    {
        $redirect_url = input("redirect_url");
        $url = url("User/address") . "?redirect_url=" . $redirect_url;
        $this->redirect($url);
    }

    /**
     * 订单支付信息
     */
    public function payInfo()
    {

        $this->templateTitle("支付");
        $order_id = input("order_id");
        //获取支付信息
        $pay_set = Settings::getSettings("pay_set");
        $this->assign("base_url", "http://" . $_SERVER["HTTP_HOST"]);
        $this->assign("pay_set", $pay_set);
        $this->assign("order_id", $order_id);
        return $this->fetch("payInfo");
    }

    /**
     * 取消订单
     */
    public function cancelOrder()
    {
        $order_id = input("order_id");
        try{
            \app\common\model\Order::orderCancel(session("user_id"), $order_id);
            $this->ajaxSuccess("取消成功");
        }catch (\Exception $e){
            $this->ajaxError($e->getMessage());
        }

    }

    /**
     * 订单收获
     */
    public function receiveOrder()
    {
        $order_id = input("order_id");
        try{
            \app\common\model\Order::orderReceive(session("user_id"), $order_id);
            $this->ajaxSuccess("操作成功");
        }catch (\Exception $e){
            $this->ajaxError($e->getMessage());
        }
    }

    /**
     * 确认支付
     */
    public function surePay()
    {
        $this->templateTitle("支付凭证");
        $order_id = input("order_id");
        $this->assign("order_id", $order_id);
        $order = db("order")->where("id", $order_id)->find();
        $this->assign("pay_img", $order["pay_img"]);
        return $this->fetch("surePay");
    }

    /**
     *
     */
    public function setOrderPayImg()
    {
        $order_id = input("order_id");
        $img_url = input("img_url");
        db("order")->where("id",$order_id)->update(["pay_img"=>$img_url]);
        $this->ajaxSuccess("上传成功");
    }

    /**
     * 订单详情
     */
    public function orderDetail($order_id)
    {

        $this->templateTitle("订单详情");
        $detail = \app\common\model\Order::orderInfo($order_id);
        $this->assign("detail", $detail);
        return $this->fetch("order_detail");
    }

    /**
     * 去评价
     */
    public function goComment()
    {
        $this->templateTitle("评价订单");
        $order_id = input("order_id");
        $this->assign("order_id", $order_id);
        $order_sn = db("order")->where("id", $order_id)->value("order_sn");
        //获取订单详情
        $order_goods = db("order_goods")->where("order_sn", $order_sn)->select();
        $this->assign("order_goods", $order_goods);
        return $this->fetch("comment");
    }

    /**
     * 订单评价
     */
    public function comment()
    {
        $order_id = input("order_id");
        $star = input("star");
        $comment = input("comment");
        try{
            $order = db("order")->where("id", $order_id)->find();
            $order_goods = db("order_goods")->where("order_sn", $order["order_sn"])->select();
            foreach ($order_goods as $item){
                //$user_id, $order_goods_id, $star, $content
                \app\common\model\Order::orderComment(session("user_id"), $item["id"], $star, $comment);
            }
            $this->ajaxSuccess("评价成功");
        }catch (\Exception $e){
            $this->ajaxError($e->getMessage());
        }

    }


}